<?php 

namespace LDL\Twspider\C;
use VDB\Spider\Stackable ;
/**
 *support $['a'] ['b'] __k__ position __cont cnot use ,sort

 */
/**
 * AbsIterator constructor.
 * @property StackArray stack
 */
class AbsIterator implements \Iterator {

		function __construct( &$stackArray){

			$this->stack= $stackArray;
			//var_dump($this->stack->keys());
		}
		function  current (){
		//	echo "current \n";
			return $this->stack->current() ;
		  }
		function key (  ){
		//	echo "key \n";
			return $this->stack->key() ;
		}
		 function next ( ){
		//	echo "next \n";
			$this->stack->next();
		 
		 }
		function rewind ( ){
		//	echo "rewind \n";
			$this->stack->rewind();
		}
		function  valid (){
		//	echo "valid \n";
			return $this->stack->valid();
		}	
};
class StackArray extends Stackable
    //implements \Iterator // 不能覆盖线程里的iter？
//	implements \ArrayAccess ,if extends threaded ,it wont  be called  , 
{

	const KEY_DEF="__k__";//存key
	private $position= 0 ;
	//private $__cont ;
	//private $KEYS_ARR=array('__k__','position','KEYS_ARR');

	private function initPosition(){
		$this->position= 0 ; //$countks > 0 ? 0 : -1;
	}
	public function sort($funName="sort"){
		$keys=$this->getKeys() ;
		call_user_func_array($funName,array(&$keys));
		$this->__k__= serialize($keys);

		//return $this;
	}
	public function dump(){
        var_dump($this->keys());
	}
public function current(){
		return $this[$this->key()];
	}
public function key(){
		$ks = $this->keys();
		return isset($ks[$this->position]) ? $ks[$this->position]:false;	
	}
public function next(){
		$countks= count($this->keys()) ;
		if( $countks > 0 
			&& 
			$countks >=($this->position+1)){
			++$this->position;
		}
	}
public function rewind(){
		$this->initPosition();
	}
public function valid(){
		$key=$this->key();
		if($key===false){
			return false ;
		}
		$countks= count($this->keys()) ;
		$res= ($countks > 0 ) && isset($this[$key]);
		
		return $res ; 
	}
	public function keys(){
	    if(empty($this->__k__)){
	        $this->sort();
        }
	  return  unserialize($this->__k__);
	}
	//拿到所有KEYS
	private function getKeys(){
		//$a=$this ;//->getCont() ; 
		// ['a'] ['b']
		//$re=empty($this->__k__) ? array():unserialize($this->__k__);
            $re=array();
			foreach($this as $k=>$v){
				if(!$this->isfromBuild($k)){
					$re[]=$k;
				}
			}

		return $re;
		
	}

	/*public function offsetGet($offset)
    {
        echo "offGet $offset\n";
        parent::offsetGet($offset); // TODO: Change the autogenerated stub
    }
    public function offsetSet($offset, $value)
    {
        echo "offSet $offset\n";
        parent::offsetSet($offset, $value); // TODO: Change the autogenerated stub
    }*/

    public function __set($key,$value){
      // echo "__set {$key} start\n";
        $this->{$key}=$value; //不会再死loop
        $this->sort();
       // echo "__set {$key} end\n";
	}
	function __get($name)
    {
       // echo "__get {$name} \n";
        return $this[$name];
    }

    public function __isset($key){
		if($this->isfromBuild($key)){
			return false;
		}else{
			return isset($this[$key]) ;
		}
	}
/**
 * 是否从CONT属性取值
 * true cont 
 * flase */
	private function isfromBuild($key){
        $is= in_array($key,array('__k__','position','KEYS_ARR'),true);
	    // echo "{$key} is build :".$is."\n";
	     return $is;
	}

	public function getIterator(){
		return new AbsIterator($this);
	}
}
